//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.hzero.plugin.platform.hr.api.controller;

import static io.choerodon.platform.infra.constants.Constants.DING_TALK;

import java.util.List;

import io.swagger.annotations.*;
import org.hzero.boot.platform.lov.annotation.ProcessLovValue;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.plugin.platform.hr.api.dto.EmployeeUserDTO;
import org.hzero.plugin.platform.hr.app.service.EmployeeUserService;
import org.hzero.plugin.platform.hr.domain.entity.EmployeeUser;
import org.hzero.plugin.platform.hr.domain.repository.EmployeeUserRepository;
import org.hzero.starter.keyencrypt.core.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.core.oauth.DetailsHelper;
import io.choerodon.platform.app.service.UserService;
import io.choerodon.swagger.annotation.Permission;

@Api(
        tags = {"Employee User"}
)
@RestController("employeeUserController.v1")
@RequestMapping({"/v1/{organizationId}/employee-users"})
public class EmployeeUserController extends BaseController {
    private EmployeeUserRepository employeeUserRepository;
    private EmployeeUserService employeeUserService;
    private UserService userService;

    @Autowired
    public EmployeeUserController(EmployeeUserRepository employeeUserRepository, EmployeeUserService employeeUserService, UserService userService) {
        this.employeeUserRepository = employeeUserRepository;
        this.employeeUserService = employeeUserService;
        this.userService = userService;
    }

    @ApiOperation("员工用户关系列表")
    @Permission(
            level = ResourceLevel.ORGANIZATION
    )
    @GetMapping
    @ApiImplicitParams({@ApiImplicitParam(
            name = "employeeId",
            value = "员工id",
            paramType = "query",
            required = true
    )})
    @ProcessLovValue(
            targetField = {"body"}
    )
    public ResponseEntity<List<EmployeeUserDTO>> pageEmployeeUser(@PathVariable @ApiParam(value = "租户ID", required = true) Long organizationId, @Encrypt Long employeeId) {
        return Results.success(this.employeeUserRepository.listByEmployeeId(organizationId, employeeId));
    }

    @ApiOperation("查询用户在某个租户下的员工信息")
    @Permission(
            level = ResourceLevel.ORGANIZATION
    )
    @GetMapping({"/employee"})
    public ResponseEntity<EmployeeUserDTO> getEmployee(@PathVariable @ApiParam(value = "租户ID", required = true) Long organizationId, @ApiParam("是否启用") @RequestParam(required = false) Integer enabledFlag, @ApiParam(hidden = true) @RequestParam(required = false) @Encrypt Long userId) {
        return Results.success(this.employeeUserService.getEmployee(userId == null ? DetailsHelper.getUserDetails().getUserId() : userId, organizationId, enabledFlag));
    }

    @ApiOperation("批量查询用户在某个租户下的员工信息")
    @Permission(
            level = ResourceLevel.ORGANIZATION
    )
    @GetMapping({"/employee/batch"})
    public ResponseEntity<List<EmployeeUserDTO>> listEmployee(@PathVariable @ApiParam(value = "租户ID", required = true) Long organizationId, @RequestParam @ApiParam(value = "用户ID列表", required = true) @Encrypt List<Long> userIdList) {
        return Results.success(this.employeeUserService.listEmployee(userIdList, organizationId));
    }

    @ApiOperation("根据员工ID列表批量查询员工(包含关联的用户明细信息)")
    @Permission(
            level = ResourceLevel.ORGANIZATION
    )
    @GetMapping({"/employee/user/batch"})
    public ResponseEntity<List<EmployeeUserDTO>> listEmployeeUser(@PathVariable @ApiParam(value = "租户ID", required = true) Long organizationId, @RequestParam @ApiParam(value = "员工ID列表", required = true) @Encrypt List<Long> employeeIds) {
        return Results.success(this.employeeUserService.listEmployeeUser(employeeIds, organizationId));
    }

    @ApiOperation("批量新增员工用户关系")
    @Permission(
            level = ResourceLevel.ORGANIZATION
    )
    @PostMapping
    public ResponseEntity<List<EmployeeUser>> batchInsertEmployees(@RequestBody @Encrypt List<EmployeeUser> employeeUsers) {
        this.validList(employeeUsers, new Class[0]);
        return Results.success(this.employeeUserService.batchInsertEmployees(employeeUsers));
    }

    @ApiOperation("批量删除员工用户关系")
    @Permission(
            level = ResourceLevel.ORGANIZATION
    )
    @DeleteMapping
    public ResponseEntity batchRemoveEmployeeUsers(@PathVariable @ApiParam(value = "租户ID", required = true) Long organizationId, @RequestBody @Encrypt List<EmployeeUser> employeeUsers) {
        this.employeeUserService.batchRemoveEmployeeUsers(organizationId, employeeUsers);
        return Results.success();
    }

    @ApiOperation("通过租户id和员工编码查询用户在某个租户下的员工信息(包含关联的用户明细信息)")
    @Permission(
            level = ResourceLevel.ORGANIZATION
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "organizationId",
            value = "租户Id",
            paramType = "path",
            required = true
    )})
    @GetMapping({"/employee-num"})
    public ResponseEntity<List<EmployeeUserDTO>> getEmpUsersByEmpNum(@PathVariable @ApiParam(value = "租户ID", required = true) Long organizationId, @RequestParam("employeeNums") List<String> employeeNums) {
        return Results.success(this.employeeUserRepository.listByEmployeeNum(organizationId, employeeNums));
    }

    @ApiOperation("通过用户id获取员工在第三方平台的id")
    @Permission(
            permissionWithin = true
    )
    @GetMapping({"/open-userid"})
    public ResponseEntity<List<String>> getOpenUserId(@PathVariable("organizationId") Long organizationId,
                                                      @RequestParam("ids") List<Long> ids) {
        return Results.success(this.userService.queryOpenAppUserIds(organizationId, ids, DING_TALK));
    }
}
